package com.mailbox.mailbox.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mailbox.mailbox.domain.SecurityUser;
import com.mailbox.mailbox.domain.User;
import com.mailbox.mailbox.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
/**
 * @author 栗守佳
 */
public class UserDetailsImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = null;
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        user=userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException("账号不存在！");
        }
        List<String> list = new ArrayList<>();
        list.add(user.getGrade() + "");
        SecurityUser securityUser = new SecurityUser(user.getUserId(),username,user.getPassword(),list);
        return securityUser;
    }
}
